15.6 Memory Reclamation
During the Linux initialization sequence, the system spawns an asynchronous kernel daemon designated as kswapd(), which remains in a low-power sleep state under normal operations. If memory resources experience severe contention or fragmentation bottlenecks throughout the system runtime, this background daemon is awakened. It processes the five globally managed LRU tracking lists to release portions of the allocated physical memory, subsequently invoking compaction routines to optimize the residual pools. This complete operational pipeline of freeing allocated frames is designated as memory reclamation.
The kswapd() daemon orchestrates memory freeing operations via the routine balance_pgdat(). When the volume of anonymous pages residing on the inactive list (the cold chain) drops beneath safety thresholds, balance_pgdat() invokes age_active_anon() to transition a portion of the active anonymous memory frames (the hot chain) down into the inactive pool. If the Memory Resource Controller (memcg) subsystem is enabled, system administrators can configure explicit soft limit thresholds for corporate memory consumption profiles. When the volume of free system memory drops below these benchmarks, balance_pgdat() triggers mem_cgroup_soft_limit_reclaim() to free physical memory allocations from the specific memcg instance that breaches its soft limit by the widest margin within that node. Concurrently, kswapd_shrink_node() focuses eviction routines on reclaiming blocks from the highest-indexed eligible memory nodes. Following the conclusion of a memory reclamation cycle, balance_pgdat() invokes wakeup_kcompactd() to alert the background memory compaction daemon. Furthermore, the kernel exposes the synchronous functions do_try_to_free_pages() and node_reclaim(), which can be explicitly called by running processes to perform direct, foreground memory reclamation passes. The core engine managing the memory reclamation subsystem is declared within the source file git/mm/vmscan.c.